<?php

/**
 * Controls the functionality  for Questions provided through the presentation layer (view)
 * to the user
 * The actions were created based on the tutorial of CakePHP Cookbook
 * http://book.cakephp.org/2.0/_downloads/en/CakePHPCookbook.pdf
 * 
 * PHP5
 * 
 * @package Controller.QuestionsController
 */
App::import('Model', 'QuestionType');

class QuestionsController extends AppController {
    
    public $helpers = array('Html', 'Form');
    public $components = array('Session');
    var $webSurveyId;
    var $position;
     
    public function delete($id, $questionPos){
        if ($this->request->is('get')){
            throw new MethodNotAllowedException();
        }
        $question = $this->Question->findById($id); 
        $position = ($questionPos == '0') ? $questionPos :  strval(intval(--$questionPos));
        if ($this->Question->delete($id)) {
            return $this->redirect(array('controller' => 'WebSurveys', 
                                        'action' => 'edit',
                                        $question['WebSurvey']['id'],
                                        $position));
        }
    }
    
    public function add($id=null, $questionPos=0) {
        
        if ($id != null){
           // $questionType = new QuestionType();
           // $questionTypes = $questionType.find('all');
            $webSurveyId = $id;
            $position = strval(intval(++$questionPos));
        //    $this->set('questionTypes', $questionTypes);
        }
            
        if ($this->request->isPost()) {
            $this->Question->create();
            
            $this->request->data['WebSurvey']['id'] = $webSurveyId;          
            
            if ($this->Question->saveAll($this->request->data)) {
                return $this->redirect(array('controller' => 'WebSurveys', 
                                             'action' => 'edit',
                                             $this->Question->WebSurvey->id, $position));
            }
        }
    }    
}
